package com.zhangfei.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class SpringSessionController {
    @GetMapping("/session")
    public String getSessionInfo(HttpSession session) {
        String attributeName = "username";
        String attributeValue = (String) session.getAttribute(attributeName);
        return "Session Attribute '" + attributeName + "': " + attributeValue;
    }

    @PostMapping("/session")
    public String setSessionInfo(@RequestParam String username, HttpSession session) {
        session.setAttribute("username", username);
        return "Session Attribute 'username' set to: " + username;
    }

}
